package main

import (
	"kill/common"
	"kill/fronted/middleware"
	"kill/fronted/web/controllers"
	"kill/repositories"
	"kill/services"

	"github.com/kataras/iris/v12"
	"github.com/kataras/iris/v12/mvc"

	"kill/rabbitmq"
)

func main() {
	//1.创建iris 实例
	app := iris.New()
	//2.设置错误模式，在mvc模式下提示错误
	app.Logger().SetLevel("debug")
	//3.注册模板
	tmplate := iris.HTML("./fronted/web/views", ".html").Layout("shared/layout.html").Reload(true)
	app.RegisterView(tmplate)
	//4.设置模板
	app.HandleDir("/public", "./fronted/web/public")
	//出现异常跳转到指定页面
	app.OnAnyErrorCode(func(ctx iris.Context) {
		ctx.ViewData("message", ctx.Values().GetStringDefault("message", "访问的页面出错！"))
		ctx.ViewLayout("")
		ctx.View("shared/error.html")
	})
	//连接数据库
	db, err := common.NewMysqlConn()
	if err != nil {
		panic(err)
	}

	user := repositories.NewUserRepository("user", db)
	userService := services.NewService(user)
	mvc.New(app.Party("/user")).Register(userService).Handle(new(controllers.UserController))

	//生成消息队列
	rabbitmq := rabbitmq.NewRabbitMQSimple("imoocProduct")
	//注册product控制器
	product := repositories.NewProductManager("product", db)
	productService := services.NewProductService(product)
	order := repositories.NewOrderMangerRepository("order", db)
	orderService := services.NewOrderService(order)
	mvc.New(app.Party("/product")).Register(productService, orderService, rabbitmq).Handle(new(controllers.ProductController))
	app.Party("/product").Use(middleware.AuthConProduct)

	app.Run(
		iris.Addr(":8082"),
		iris.WithoutServerError(iris.ErrServerClosed),
		iris.WithOptimizations,
	)
}
